package dao.impl;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

import dao.ProviderDAO;
import model.Provider;

public class ProviderDAOImpl implements ProviderDAO{

	private HashMap<String, Provider> providers;
    private int 	SeqId = 0;
    private int 	version = 1;
	
    public ProviderDAOImpl() {
    	if (providers.size() == 0) {
			add("NPI_1", "name1", "sex1","loc1", "zip1", "long1", "lat1", "specialty1");
			add("NPI_2", "name2", "sex2","loc2", "zip2", "long22", "lat22", "specialty2");
			add("NPI_3", "name3", "sex3","loc3", "zi33", "long3", "lat3",  "specialty3");
		
		}
   	    System.out.println("Creating Provider DAO Implementation");
    }
 
    // create
    public boolean add(Provider provider) {
	  	provider.setId(SeqId);
		providers.put(provider.getNPI(), provider);
        SeqId++;
		return true;
	}
	public boolean add(String NPI, String name, String gender, String location,
			String zipcode, String longitude, String latitude,  String specialty) {
		Provider p = new Provider(null, version, NPI, name, gender, location,
					zipcode, longitude, latitude,  specialty );
		return add(p);
	}
	
	
	
    public Provider[] getAll() {
    	Collection<Provider> collection = providers.values();
		return collection.toArray(new Provider[collection.size()]);
	}	
       
  	public long getCount() {
		return providers.size();
	}
	
  	public Provider getById(Integer id) {
		return providers.get(id);
	}

	public Provider getByNPI(String NPI) {
		Provider provider = null;
		for (Provider p : providers.values()) {
			if (p.getNPI().equals(NPI)) {
				provider = p;
				break;
			}
		}
		return provider;
	}
	
	public Provider[] getByName(String name) {
		List<Provider> found = new ArrayList<Provider>();
		for (Provider p : providers.values()) {
			if (p.getName().equals(name)) {
				found.add(p);
			}
		}
		return found.toArray(new Provider[found.size()]);
	}
	public Provider[] getByGender(String gender) {
		List<Provider> found = new ArrayList<Provider>();
		for (Provider p : providers.values()) {
			if (p.getGender().equals(gender)) {
				found.add(p);
			}
		}
		return found.toArray(new Provider[found.size()]);
	}
	public Provider[] getByLocation(String location) {
		List<Provider> found = new ArrayList<Provider>();
		for (Provider p : providers.values()) {
			if (p.getLocation().equals(location)) {
				found.add(p);
			}
		}
		return found.toArray(new Provider[found.size()]);
	}
	public Provider[] getByZipcode(String zipcode) {
		List<Provider> found = new ArrayList<Provider>();
		for (Provider p : providers.values()) {
			if (p.getZipcode().equals(zipcode)) {
				found.add(p);
			}
		}
		return found.toArray(new Provider[found.size()]);
	}	public Provider[] getBySpecialty(String specialty) {
		List<Provider> found = new ArrayList<Provider>();
		for (Provider p : providers.values()) {
			if (p.getSpecialty().equals(specialty)) {
				found.add(p);
			}
		}
		return found.toArray(new Provider[found.size()]);
	}
	
	public boolean update(Provider provider) {
    	providers.put(provider.getNPI(), provider);
		return false;
	}
	
	public boolean delete(Provider provider) {
		providers.remove(provider.getNPI());
	    return true;
	}
	
    public Provider save(Provider provider) {
    	provider.setId(SeqId);
    	providers.put(provider.getNPI(), provider);
        SeqId++;
		return provider;
	}
  
}
    